<?php
class Admin_NewsController extends Zend_Controller_Action
{
	public function init()
    {
  
	}
	
	public function indexAction()
    {
		$storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(!$data){
            $this->_redirect('admin/auth/login');
        }        
	    $this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$modNews = new News();
		$request = $this->getRequest();
		$stat = $request->getParam('stat');
		$news_arr = $modNews->listNews();
		$news_arr_main = $modNews->getMainDetails();
		$this->view->title = "News Manager";
		$this->view->news_arr = $news_arr;
		$this->view->news_arr_main = $news_arr_main;
		$this->view->stat = $stat;
	}
	
	public function addAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/forms/News.php");
		$controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $newsform = new Form_News($options);
		$this->view->title = "Add News";		
        $this->view->newsform = $newsform;
	}
	
	public function doaddAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/forms/News.php");
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$newsform = new Form_News();
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) { 
			$formdata = $request->getPost();
			if($newsform->isValid($formdata)){           
				$modNews = new News();			
				$addnews = $modNews->addNews($formdata);
				$this->_redirect('/admin/news/index/stat/added');			
			} else {
               $newsform->populate($formdata);               
               $this->view->newsform = $newsform;
              }
		}
	}
	
	public function editAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/models/News.php");		
		Zend_Loader::loadFile("../application/admin/forms/News.php");		
		$request = $this->getRequest();
		$news_id = $request->getParam('id');
		$modNews = new News();
		
		
		
		$controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $id = $this->_getParam('id', 0);
        $options = array("action"=>$action, "id"=>$id);
		$newsform = new Form_News($options);
		$news_arr = $modNews->getNewsById($news_id);
		$this->view->title = "Add News";		
        $this->view->newsform = $newsform;
        $this->view->news_id = $news_id;
        if ($news_id > 0) {            
            $newsform->populate($news_arr);                
        } 
	}
	
	public function doeditAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/forms/News.php");
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$newsform = new Form_News();
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) {            
			$modNews = new News();
			$form_values = $request->getPost();
			$update_news = $modNews->updateNews($form_values);
			$this->_redirect('/admin/news/index/stat/updated');				
		}
	}
	
	public function changestatusAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$modNews = new News();
		$request = $this->getRequest();
		$news_id = $request->getParam('news_id');
		$modNews->changeNewsStatus($news_id);
	}
	
	public function deleteAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$modNews = new News();
		$request = $this->getRequest();
		$news_id = $request->getParam('news_id');
		$modNews->deleteNews($news_id);
	}
	
	public function defaultdataAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$modNews = new News();
			$form_values = $request->getPost();
			$add_news = $modNews->addDefaultContent($form_values);			
			
		}
	}
	
	public function editdefaultcontentAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$request = $this->getRequest();
		$modNews = new News();
		$default_news_arr = $modNews->getMainDetails();	
		$this->view->default_news_arr = $default_news_arr;					
	}
	
	public function updatedefaultdataAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/News.php");
		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$modNews = new News();
			$form_values = $request->getPost();
			$add_news = $modNews->addDefaultContent($form_values);			
			
		}
	}
}
